<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Activity extends Admin_Controller {
	
	private $rice_export = 1;
	private $exhibition = 2;
	private $arr = array('rice_export' => 1, 'exhibition' => 2);
	
	public function __construct(){
        parent::__construct();
		$this->load->model('mod_activity');
	}
	
 	public function index(){
		$this->rice_export();	
	}
   
   	public function rice_export(){	
        $config['base_url'] = base_url() . 'activity/activity/rice_export/';
        $config['total_rows'] = $this->mod_activity->getActivityNum($this->rice_export);
		$config['uri_segment'] = 4;
        $config['per_page'] = 25;

        $this->pagination->initialize($config);
		$data['data'] = $this->mod_activity->getActivity($this->uri->segment(4), $config['per_page'], $this->rice_export);
						
		$data['title']	= 'Rice Export Activities';
		$data['page']	= 'activity/view';
		$data['action']	= 'Rice Export Activities';
		$data['method'] = 'rice_export';
    	$this->load->view('layout/admin',$data);
   	}
   
   	public function exhibition(){	
        $config['base_url'] = base_url() . 'activity/activity/rice_export/';
        $config['total_rows'] = $this->mod_activity->getActivityNum($this->exhibition);
		$config['uri_segment'] = 4;
        $config['per_page'] = 25;

        $this->pagination->initialize($config);
		$data['data'] = $this->mod_activity->getActivity($this->uri->segment(4), $config['per_page'], $this->exhibition);
								
		$data['title']	= 'Exhibition Activities';
		$data['page']	= 'activity/view';
		$data['action']	= 'Exhibition Activities';
		$data['method'] = 'exhibition';
    	$this->load->view('layout/admin',$data);
   	}
	
	public function add(){
		$action = $this->uri->segment(4);
					
		if($this->input->post()){
        	$this->form_validation->set_rules('title', 'Title', 'trim|required');
        	$this->form_validation->set_rules('description', 'Description', 'trim');
			
        	if ($this->form_validation->run() == TRUE) {
	
				$config['upload_path'] = ACTIVITY_IMAGE_PATH;
				$config['allowed_types'] = 'gif|jpg|jpeg|png|JPEG|JPG|PNG|x-png';
				$config['max_size']	= '1024';
				//$config['max_width'] = '900';
				//$config['max_height'] = '900';
			
				$this->load->library('upload', $config);
		
				if ($this->upload->do_upload('image')){				
					$image = $this->upload->data();
					$imageName = $image['file_name'];
					resize_image($imageName, 120, 160, $config['upload_path']);
						
					$title = $this->input->post('title');
					$description = $this->input->post('description');
					$type = $this->arr[$action];
					
					if($this->mod_activity->insert($imageName, $title, $description, $type) > 0){
						$this->session->set_userdata('ms', 'Successfully add new activity!');
						redirect('activity/activity/' . $action);
					} else {
						$this->session->set_userdata('ms', 'Could not add new activity, please try again!');
					}
				} else {
					$this->session->set_userdata('ms', $this->upload->display_errors());
				}
			} else {
				$this->session->set_userdata('ms', validation_errors());
			}
		}
		
		$data['title']="Add New Activity";
		$data['page'] = 'activity/new';
		$data['action']='Add New Activity';
		$data['method'] = $action;
    	$this->load->view('layout/admin',$data);
	}
	
	public function edit(){
		$action = $this->uri->segment(4);
		$pageNumber = $this->uri->segment(5);
		$id = $this->uri->segment(6);
		
		$row = $this->mod_activity->getActivityById($id);
					
		if($this->input->post()){
        	$this->form_validation->set_rules('title', 'Title', 'trim|required');
        	$this->form_validation->set_rules('description', 'Description', 'trim');
			
        	if ($this->form_validation->run() == TRUE) {
				$title = $this->input->post('title');
				$description = $this->input->post('description');
				$type = $this->arr[$action];
	
				$config['upload_path'] = ACTIVITY_IMAGE_PATH;
				$config['allowed_types'] = 'gif|jpg|jpeg|png|JPEG|JPG|PNG|x-png';
				$config['max_size']	= '1024';
				//$config['max_width'] = '900';
				//$config['max_height'] = '900';
			
				$this->load->library('upload', $config);
		
				if ($this->upload->do_upload('image')){				
					$image = $this->upload->data();
					$imageName = $image['file_name'];
					resize_image($imageName, 120, 160, $config['upload_path']);
											
					if($this->mod_activity->update($id, $imageName, $title, $description, $type) > 0){
						$this->session->set_userdata('ms', 'Successfully edit activity!');
						delete_file(ACTIVITY_IMAGE_PATH . $row[field('image')]);
						delete_file(ACTIVITY_IMAGE_PATH . 'thumbs/' . $row[field('image')]);
						redirect('activity/activity/' . $action . '/' . $pageNumber);
					} else {
						$this->session->set_userdata('ms', 'Could not edit activity, please try again!');
					}
				} else {
					//$this->session->set_userdata('ms', $this->upload->display_errors());
										
					if($this->mod_activity->update($id, '', $title, $description, $type) > 0){
						$this->session->set_userdata('ms', 'Successfully edit activity!');
						redirect('activity/activity/' . $action . '/' . $pageNumber);
					} else {
						$this->session->set_userdata('ms', 'Could not edit activity, please try again!');
					}
				}
			} else {
				$this->session->set_userdata('ms', validation_errors());
			}
		}
		$data['row'] = $row;
		
		$data['title']="Edit Activity";
		$data['page'] = 'activity/edit';
		$data['action']='Edit Activity';
		$data['method'] = $action;
    	$this->load->view('layout/admin',$data);
	}
	
	public function delete(){
		$action = $this->uri->segment(4);
		$pageNumber = $this->uri->segment(5);
		$id = $this->uri->segment(6);
		
		$data = $this->mod_activity->getActivityById($id);
		
        if ($this->mod_activity->delete($id) > 0) {
            $this->session->set_userdata('ms', 'Successfully delete!');
			delete_file(ACTIVITY_IMAGE_PATH . $data[field('image')]);
			delete_file(ACTIVITY_IMAGE_PATH . 'thumbs/' . $data[field('image')]);
        }else{
            $this->session->set_userdata('ms', 'Could not delete, please try again!');
		}
        redirect('activity/activity/' . $action . '/' . $pageNumber);
	}
}